package controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class TranslateServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		TreeMap arrLan = new TreeMap();
		arrLan.put("en", "UTF-8");
		arrLan.put("es", "ISO-8859-1");
		arrLan.put("af", "ISO-8859-1");
		arrLan.put("sq", "ISO-8859-1");
		arrLan.put("ar", "windows-1256");
		arrLan.put("hy", "UTF-8");
		arrLan.put("az", "ISO-8859-9");
		arrLan.put("eu", "ISO-8859-1");
		arrLan.put("be", "windows-1251");
		arrLan.put("bn", "ISO-8859-1");
		arrLan.put("bg", "windows-1251");
		arrLan.put("ca", "UTF-8");
		arrLan.put("zh-CN", "GB2312");
		arrLan.put("hr", "ISO-8859-2");
		arrLan.put("cs", "ISO-8859-2");
		arrLan.put("da", "ISO-8859-1");
		arrLan.put("nl", "ISO-8859-1");
		arrLan.put("eo", "ISO-8859-1");
		arrLan.put("et", "windows-1257");
		arrLan.put("tl", "ISO-8859-1");
		arrLan.put("fi", "ISO-8859-1");
		arrLan.put("fr", "ISO-8859-1");
		arrLan.put("gl", "ISO-8859-1");
		arrLan.put("ka", "UTF-8");
		arrLan.put("de", "ISO-8859-1");
		arrLan.put("el", "ISO-8859-7");
		arrLan.put("gu", "ISO-8859-1");
		arrLan.put("ht", "ISO-8859-1");
		arrLan.put("iw", "windows-1255");
		arrLan.put("hi", "ISO-8859-1");
		arrLan.put("hu", "ISO-8859-2");
		arrLan.put("is", "ISO-8859-1");
		arrLan.put("id", "ISO-8859-1");
		arrLan.put("ga", "ISO-8859-1");
		arrLan.put("it", "ISO-8859-1");
		arrLan.put("ja", "Shift_JIS");
		arrLan.put("kn", "ISO-8859-1");
		arrLan.put("ko", "EUC-KR");
		arrLan.put("la", "ISO-8859-1");
		arrLan.put("lv", "windows-1257");
		arrLan.put("lt", "windows-1257");
		arrLan.put("mk", "windows-1251");
		arrLan.put("ms", "ISO-8859-1");
		arrLan.put("mt", "ISO-8859-1");
		arrLan.put("no", "ISO-8859-1");
		arrLan.put("fa", "windows-1256");
		arrLan.put("pl", "ISO-8859-2");
		arrLan.put("pt", "ISO-8859-1");
		arrLan.put("ro", "ISO-8859-2");
		arrLan.put("ru", "windows-1251");
		arrLan.put("sr", "ISO-8859-2");
		arrLan.put("sk", "ISO-8859-2");
		arrLan.put("sl", "ISO-8859-2");
		arrLan.put("sw", "ISO-8859-1");
		arrLan.put("sv", "ISO-8859-1");
		arrLan.put("ta", "ISO-8859-1");
		arrLan.put("te", "ISO-8859-1");
		arrLan.put("th", "windows-874");
		arrLan.put("tr", "ISO-8859-9");
		arrLan.put("uk", "windows-1251");
		arrLan.put("ur", "windows-1256");
		arrLan.put("vi", "ISO-8859-1");
		arrLan.put("cy", "ISO-8859-1");
		arrLan.put("yi", "windows-1255");
		
		String content="";
		String language = request.getParameter("language");
		String urls = request.getParameter("url");
		
		String link = "http://translate.google.com/translate?hl=en&rurl=translate.google.com&sl=auto&tl="+language+"&twu=1&u="+urls;
		String charset="utf-8";
		URL url;
		try {
			Random random = new Random();
			List<String> listSock = new ArrayList<String>();
			listSock.add("184.107.74.40");
			listSock.add("184.107.74.41");
			listSock.add("184.107.74.42");
			listSock.add("184.107.74.43");
			listSock.add("184.107.74.44");
			listSock.add("184.107.74.45");
			listSock.add("184.107.74.46");
			listSock.add("184.107.74.47");
			int rand = new Random().nextInt(7);
			Socket sockket = new Socket(Utils.fomatDomain(link), 80, InetAddress.getByName(listSock.get(rand)), random.nextInt(1000)+30000);
			
//			Socket sockket = new Socket("translate.google.com", 80);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sockket.getOutputStream()));
			
			writer.write("GET "+link+" HTTP/1.1\r\n");
			writer.write("Host: "+Utils.fomatDomain(link)+"\r\n");
			writer.write("Referer: http://5ty.org\r\n");
			writer.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
			writer.write("Accept-Language: en-us,en;q=0.5\r\n");
			writer.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
			writer.write("Connection: close\r\n");
			writer.write("User-Agent:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1\r\n");
			writer.write("Cookie:	PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES\r\n");
			writer.write("\r\n\r\n");
			writer.flush();
			
			BufferedReader reader =  new BufferedReader(new InputStreamReader(sockket.getInputStream()));
			
			char[] buf = new char[102102];
			int numRead=0;
			while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
			
			Document doc = Jsoup.parse(content);
			
			link = "http://translate.google.com"+doc.select("iframe").attr("src");
	        
			rand = new Random().nextInt(7);
			sockket = new Socket(Utils.fomatDomain(link), 80, InetAddress.getByName(listSock.get(rand)), random.nextInt(1000)+30000);
			writer = new BufferedWriter(new OutputStreamWriter(sockket.getOutputStream()));
			
			writer.write("GET "+link+" HTTP/1.1\r\n");
			writer.write("Host: "+Utils.fomatDomain(link)+"\r\n");
			writer.write("Referer: http://5ty.org\r\n");
			writer.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
			writer.write("Accept-Language: en-us,en;q=0.5\r\n");
			writer.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
			writer.write("Connection: close\r\n");
			writer.write("User-Agent:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1\r\n");
			writer.write("Cookie:	PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES\r\n");
			writer.write("\r\n\r\n");
			writer.flush();
			
			reader =  new BufferedReader(new InputStreamReader(sockket.getInputStream()));
			
			buf = new char[102102];
			numRead=0;
			String location = "";
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            int start = readData.indexOf("Location:");
	            int end = readData.indexOf("Pragma:");
	            location = readData.substring(start+10,end);
	            break;
	        }
	        reader.close();
	        
	        
	        rand = new Random().nextInt(7);
			sockket = new Socket(Utils.fomatDomain(link), 80, InetAddress.getByName(listSock.get(rand)), random.nextInt(1000)+30000);
			writer = new BufferedWriter(new OutputStreamWriter(sockket.getOutputStream()));
			
			writer.write("GET "+location+" HTTP/1.1\r\n");
			writer.write("Host: "+Utils.fomatDomain(link)+"\r\n");
			writer.write("Referer: http://5ty.org\r\n");
			writer.write("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
			writer.write("Accept-Language: en-us,en;q=0.5\r\n");
			writer.write("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
			writer.write("Connection: close\r\n");
			writer.write("User-Agent:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1\r\n");
			writer.write("Cookie:	PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES\r\n");
			writer.write("\r\n\r\n");
			writer.flush();
			
			
			writer.write("\r\n\r\n");
			writer.flush();
			
			reader =  new BufferedReader(new InputStreamReader(sockket.getInputStream(),(String)arrLan.get(language)));
			
			buf = new char[102102];
			numRead=0;
			content = "";
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
	        
//	        System.out.println(content);
		}  catch (Exception e) {
			
			e.printStackTrace();
		}
		response.getWriter().println(content);
		
	}
}
